{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import numpy as np \n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '../Baselines/EnemyPositionPrediction/checkpoints/CNN_GRU/all/loss.json'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 18\u001b[0m\n\u001b[0;32m      6\u001b[0m CNN_GRU_loss \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m/CNN_GRU/all/loss.json\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m## Res_tensor 模型\u001b[39;00m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;66;03m# with open(file_head + Res_tensor_loss,'r') as f:\u001b[39;00m\n\u001b[0;32m     10\u001b[0m \u001b[38;5;66;03m#     Res_tensor_loss_file = json.load(f)\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     16\u001b[0m \u001b[38;5;66;03m#     Res_tensor_vector_loss_file = json.load(f)\u001b[39;00m\n\u001b[0;32m     17\u001b[0m \u001b[38;5;66;03m# CNN_GRU 模型\u001b[39;00m\n\u001b[1;32m---> 18\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mfile_head\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mCNN_GRU_loss\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[0;32m     19\u001b[0m     CNN_GRU_loss_file \u001b[38;5;241m=\u001b[39m json\u001b[38;5;241m.\u001b[39mload(f)\n",
      "File \u001b[1;32mc:\\Users\\lenovo\\.conda\\envs\\torch_wargame\\lib\\site-packages\\IPython\\core\\interactiveshell.py:324\u001b[0m, in \u001b[0;36m_modified_open\u001b[1;34m(file, *args, **kwargs)\u001b[0m\n\u001b[0;32m    317\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[0;32m    318\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m    319\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    320\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    321\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    322\u001b[0m     )\n\u001b[1;32m--> 324\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m io_open(file, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '../Baselines/EnemyPositionPrediction/checkpoints/CNN_GRU/all/loss.json'"
     ]
    }
   ],
   "source": [
    " \n",
    "file_head = '../Baselines/EnemyPositionPrediction/checkpoints'\n",
    "\n",
    "## 可更换文件位置 /ELPNet/full/all/loss.json, /ELPNet/trans/all/loss.json, /ELPNet/gnn/all/loss.json, /ELPNet/base/all/loss.json\n",
    "ELP_loss = '/ELPNet/full/all/loss.json'    \n",
    "\n",
    "# ELPNet 模型\n",
    "with open(file_head + ELP_loss,'r') as f:\n",
    "    ELP_loss_file = json.load(f)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def smooth(data_np, n_point=1000):\n",
    "#     weight=np.ones(n_point)/n_point\n",
    "    last = np.ones(n_point-1)*data_np[0]\n",
    "    data_np = np.concatenate((last,data_np),axis=0)\n",
    "    smoothed = []\n",
    "    for index in range(len(data_np)-n_point+1):\n",
    "        \n",
    "        smoothed_val = data_np[index:index+n_point].mean()\n",
    "        smoothed.append(smoothed_val)\n",
    "    return np.array(smoothed)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "52515\n",
      "13018\n"
     ]
    }
   ],
   "source": [
    "# ELPNet 模型\n",
    "ELP_record_loss_mean, \\\n",
    "ELP_record_loss, \\\n",
    "ELP_record_loss_step, \\\n",
    "ELP_record_acc,\\\n",
    "ELP_record_acc_step = smooth(np.array(ELP_loss_file['record_loss'])), \\\n",
    "                            np.array(ELP_loss_file['record_loss']),\\\n",
    "                            np.arange(len(ELP_loss_file['record_loss_step'])),\\\n",
    "                            np.array(ELP_loss_file['record_acc']), \\\n",
    "                            np.arange(len(ELP_loss_file['record_acc_step']))\n",
    "\n",
    "\n",
    "\n",
    "print(len(ELP_record_loss_step))\n",
    "\n",
    "# loss_index1 = np.arange(2*len(ELP_record_loss_step))\n",
    "loss_index = np.arange(len(ELP_record_loss_step))\n",
    "\n",
    "#######  添加\n",
    "print(len(ELP_record_acc_step))\n",
    "acc_index = np.arange(len(ELP_record_acc_step))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'plt' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m fig \u001b[38;5;241m=\u001b[39m \u001b[43mplt\u001b[49m\u001b[38;5;241m.\u001b[39mfigure()\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# plt.title(\"Mean loss of training\") \u001b[39;00m\n\u001b[0;32m      3\u001b[0m plt\u001b[38;5;241m.\u001b[39mxlabel(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mstep\u001b[39m\u001b[38;5;124m\"\u001b[39m) \n",
      "\u001b[1;31mNameError\u001b[0m: name 'plt' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "fig = plt.figure()\n",
    "# plt.title(\"Mean loss of training\") \n",
    "plt.xlabel(\"step\") \n",
    "plt.ylabel(\"cross entropy\") \n",
    "\n",
    "\n",
    "plt.plot(loss_index, ELP_record_loss[loss_index],linewidth=0.5) \n",
    "plt.plot(loss_index, ELP_record_loss_mean[loss_index], label='ELP-Net', color = 'b',linewidth=1) \n",
    "\n",
    "\n",
    "plt.legend()\n",
    "plt.axis([-1000, 40000, -0.5, 9])\n",
    "plt.show()\n",
    "fig.savefig('loss.png',format='png')\n",
    "fig.savefig('loss.eps',format='eps')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'plt' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m fig \u001b[38;5;241m=\u001b[39m \u001b[43mplt\u001b[49m\u001b[38;5;241m.\u001b[39mfigure()\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# plt.title(\"Mean accuracy of training\") \u001b[39;00m\n\u001b[0;32m      3\u001b[0m plt\u001b[38;5;241m.\u001b[39mxlabel(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mepisodes\u001b[39m\u001b[38;5;124m\"\u001b[39m) \n",
      "\u001b[1;31mNameError\u001b[0m: name 'plt' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "fig = plt.figure()\n",
    "# plt.title(\"Mean accuracy of training\") \n",
    "plt.xlabel(\"episodes\") \n",
    "plt.ylabel(\"accuracy\") \n",
    "\n",
    "plt.plot(acc_index,smooth(ELP_record_acc[acc_index], 100), label='ELP-Net') \n",
    "\n",
    "plt.axis([0, 8000, 0, 1])\n",
    "plt.legend(loc='upper center', bbox_to_anchor=(0.5,1),ncol=1,fancybox=True,shadow=False)\n",
    "# plt.legend()\n",
    "plt.show()\n",
    "fig.savefig('acc.png',format='png')\n",
    "fig.savefig('acc.eps',format='eps')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import numpy as np \n",
    "from matplotlib import pyplot as plt "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "file_head = '../Baselines/EnemyPositionPrediction/checkpoints'\n",
    "ELP_result = '/ELPNet/full/all/test/test_result'\n",
    "\n",
    "\n",
    "with open(file_head + ELP_result,'rb') as f:\n",
    "    ELP_result_file = pickle.load(f)\n",
    "\n",
    "\n",
    "ELP_path = '/ELPNet/full/all/test/path_list'\n",
    "\n",
    "with open(file_head + ELP_path,'rb') as f:\n",
    "    ELP_path_file = pickle.load(f)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_max_acc(result_file, path_file, offset=0):\n",
    "    acc_n = []\n",
    "    for dic in result_file:\n",
    "        acc = [[] for _ in range(7)]\n",
    "        for piece in range(6):\n",
    "            action_pre=dic['action_pre_per_replay'][piece]\n",
    "            action_gt = dic['action_gt_per_replay'][piece]\n",
    "            for action_pre_per_replay, action_gt_per_replay in zip(action_pre, action_gt):\n",
    "                acc_per_replay = np.mean(action_pre_per_replay[offset:] == action_gt_per_replay[offset:])\n",
    "                acc[piece].append(acc_per_replay)\n",
    "\n",
    "        for piece in range(6):\n",
    "            acc[piece] = np.array(acc[piece]).mean()\n",
    "        acc[6] = np.array(acc[:6]).mean()\n",
    "        acc_n.append(acc)\n",
    "    acc_n = np.array(acc_n)\n",
    "    \n",
    "    for i in range(acc_n.shape[0]):\n",
    "        print(acc_n[i], path_file[i])  \n",
    "    max_row = np.argmax(acc_n[:,-1])\n",
    "    print(path_file[max_row])\n",
    "    ##################################\n",
    "    steps = 10\n",
    "    dic_max = result_file[max_row]\n",
    "    \n",
    "    acc_n_step = []\n",
    "    action_pre_result, action_gt_result = [[] for _ in range(steps)], [[] for _ in range(steps)]\n",
    "    for piece in range(6):\n",
    "        action_pre = dic_max['action_pre_per_replay'][piece]\n",
    "        action_gt = dic_max['action_gt_per_replay'][piece]\n",
    "        for action_pre_per_replay, action_gt_per_replay in zip(action_pre, action_gt):\n",
    "            if len(action_pre_per_replay[offset:]) < steps:\n",
    "                continue\n",
    "\n",
    "            step = len(action_pre_per_replay[offset:]) // steps\n",
    "            for s in range(steps):\n",
    "                action_pre_result[s].append(action_pre_per_replay[offset:][s * step:(s + 1) * step])\n",
    "                action_gt_result[s].append(action_gt_per_replay[offset:][s * step:(s + 1) * step])\n",
    "\n",
    "        X = np.arange(steps)\n",
    "        Y = np.zeros(steps)\n",
    "        for idx, (action_pres, action_gts) in enumerate(zip(action_pre_result, action_gt_result)):\n",
    "\n",
    "            action_pres_np = np.hstack(action_pres)\n",
    "            action_gts_np = np.hstack(action_gts)\n",
    "\n",
    "            Y[idx] = np.mean(action_pres_np == action_gts_np)\n",
    "        acc_n_step.append(Y)\n",
    "    return acc_n, acc_n[max_row, :], np.array(acc_n_step)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.27906341 0.24628919 0.24858493 0.28625769 0.25242821 0.29660851\n",
      " 0.26820532] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1680480.pth\n",
      "[0.28176386 0.24212984 0.25883947 0.28032756 0.26300598 0.29384742\n",
      " 0.26998569] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1650528.pth\n",
      "[0.27405225 0.24619773 0.24974883 0.27376781 0.25251396 0.28819829\n",
      " 0.26407981] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1600512.pth\n",
      "[0.28155891 0.24698434 0.25462867 0.27983122 0.26205304 0.29325191\n",
      " 0.26971801] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1550496.pth\n",
      "[0.28075498 0.24265648 0.25018639 0.27996168 0.25665222 0.29870508\n",
      " 0.2681528 ] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1500480.pth\n",
      "[0.28135178 0.24750152 0.25407168 0.27887831 0.26196392 0.29254749\n",
      " 0.26938578] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1450464.pth\n",
      "[0.28371037 0.24493913 0.26353223 0.29230815 0.26936652 0.30648025\n",
      " 0.27672277] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1400448.pth\n",
      "[0.2766249  0.2495049  0.25936265 0.28307554 0.26546399 0.2995812\n",
      " 0.27226886] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1350432.pth\n",
      "[0.29590525 0.24198764 0.26468575 0.28195382 0.26940777 0.29153886\n",
      " 0.27424652] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1300416.pth\n",
      "[0.27462017 0.24688074 0.26037071 0.27646859 0.26189201 0.28700854\n",
      " 0.26787346] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1250400.pth\n",
      "[0.29516369 0.25083912 0.24354475 0.28871041 0.25532106 0.30035022\n",
      " 0.27232154] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1200384.pth\n",
      "[0.29076478 0.2403969  0.2629416  0.27298291 0.27336383 0.29251711\n",
      " 0.27216119] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1150368.pth\n",
      "[0.29298331 0.25151802 0.26173024 0.28262121 0.27331666 0.2977581\n",
      " 0.27665459] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1100352.pth\n",
      "[0.28302009 0.24765855 0.26790161 0.29319637 0.27744332 0.31226077\n",
      " 0.28024679] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1050336.pth\n",
      "[0.28859641 0.25122696 0.28369477 0.28426231 0.29160636 0.30678689\n",
      " 0.28436228] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_1000320.pth\n",
      "[0.28640334 0.25659535 0.26145593 0.27255541 0.27343884 0.29663026\n",
      " 0.27451319] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_950304.pth\n",
      "[0.28935857 0.26764073 0.27232372 0.28806434 0.27584378 0.30675378\n",
      " 0.28333082] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_900288.pth\n",
      "[0.25407336 0.26669041 0.2895131  0.29051869 0.29760064 0.29697089\n",
      " 0.28256118] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_850272.pth\n",
      "[0.29818145 0.26873069 0.28821319 0.30266645 0.2998503  0.31760039\n",
      " 0.29587374] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_800256.pth\n",
      "[0.27150394 0.24922594 0.248078   0.24314653 0.24758918 0.25588684\n",
      " 0.25257174] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_750240.pth\n",
      "[0.29602026 0.24497009 0.24795311 0.28648559 0.25183178 0.29245996\n",
      " 0.26995347] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_700224.pth\n",
      "[0.2930805  0.2439287  0.28238843 0.29041106 0.29232847 0.30133734\n",
      " 0.28391241] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_650208.pth\n",
      "[0.23353375 0.2759867  0.25672337 0.27642522 0.27641586 0.29391246\n",
      " 0.26883289] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_600192.pth\n",
      "[0.27651245 0.27190634 0.29218469 0.29584026 0.27962105 0.29945038\n",
      " 0.2859192 ] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_550176.pth\n",
      "[0.29092992 0.25471992 0.27293003 0.26510301 0.25746971 0.27091128\n",
      " 0.26867731] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_500160.pth\n",
      "[0.2673514  0.24446445 0.26737584 0.29303346 0.23649749 0.31571362\n",
      " 0.27073938] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_450144.pth\n",
      "[0.22429235 0.2320257  0.19543846 0.2211011  0.18554809 0.23334386\n",
      " 0.21529159] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_400128.pth\n",
      "[0.28246736 0.21687967 0.19014428 0.27908238 0.21565246 0.28819849\n",
      " 0.24540411] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_350112.pth\n",
      "[0.26681017 0.24985361 0.20210142 0.27203389 0.18717942 0.2820766\n",
      " 0.24334252] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_300096.pth\n",
      "[0.23117647 0.21943553 0.28187911 0.21717221 0.27092356 0.2902\n",
      " 0.25179781] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_250080.pth\n",
      "[0.24842596 0.20110766 0.18419387 0.13971357 0.17180989 0.16786614\n",
      " 0.18551951] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_200064.pth\n",
      "[0.16605884 0.20263005 0.26389153 0.22719755 0.26512728 0.24588228\n",
      " 0.22846459] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_150048.pth\n",
      "[0.2434606  0.19126046 0.21195331 0.22997433 0.21757061 0.24381277\n",
      " 0.22300535] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_100032.pth\n",
      "[0.24988554 0.19400004 0.27315841 0.1955316  0.24843785 0.24350332\n",
      " 0.23408613] checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_50016.pth\n",
      "checkpoints\\CNN_GRU\\all\\snapshots\\model_iter_800256.pth\n",
      "[0.29818145 0.26873069 0.28821319 0.30266645 0.2998503  0.31760039\n",
      " 0.29587374]\n"
     ]
    }
   ],
   "source": [
    "ELP_acc, ELP_acc_max, ELP_acc_n_step = get_max_acc(ELP_result_file, ELP_path_file, offset=0)\n",
    "\n",
    "\n",
    "print(ELP_acc_max)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'plt' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m fig \u001b[38;5;241m=\u001b[39m \u001b[43mplt\u001b[49m\u001b[38;5;241m.\u001b[39mfigure()\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# plt.title(\"Mean accuracy of training\") \u001b[39;00m\n\u001b[0;32m      3\u001b[0m plt\u001b[38;5;241m.\u001b[39mxlabel(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnetwork parameters with time\u001b[39m\u001b[38;5;124m\"\u001b[39m) \n",
      "\u001b[1;31mNameError\u001b[0m: name 'plt' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "fig = plt.figure()\n",
    "# plt.title(\"Mean accuracy of training\") \n",
    "plt.xlabel(\"network parameters with time\") \n",
    "plt.ylabel(\"accuracy\") \n",
    "\n",
    "plt.plot(np.arange(len(ELP_acc)),ELP_acc[:,-1], '-^', label='ELPNet') \n",
    "\n",
    "plt.axis([0, 40, 0.15, 0.8])\n",
    "plt.legend(loc='upper center', bbox_to_anchor=(0.8,1),ncol=1,fancybox=True,shadow=False)\n",
    "# plt.legend()\n",
    "plt.show()\n",
    "fig.savefig('acc_parameter.png',format='png')\n",
    "fig.savefig('acc_parameter.eps',format='eps')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'plt' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m N \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m7\u001b[39m\n\u001b[1;32m----> 2\u001b[0m fig \u001b[38;5;241m=\u001b[39m \u001b[43mplt\u001b[49m\u001b[38;5;241m.\u001b[39mfigure()\n\u001b[0;32m      3\u001b[0m index \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marange(\u001b[38;5;241m0\u001b[39m, N, \u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m      4\u001b[0m \u001b[38;5;28mprint\u001b[39m(ELP_acc_max)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'plt' is not defined"
     ]
    }
   ],
   "source": [
    "N = 7\n",
    "fig = plt.figure()\n",
    "index = np.arange(0, N, 1)\n",
    "print(ELP_acc_max)\n",
    "tick_label = ['tank_1','tank_2','armored\\nvehicle_1','armored\\nvehicle_2','soldier_1','soldier_2','mean']; \n",
    "bar_width = 0.3\n",
    "\n",
    "plt.bar(index , ELP_acc_max, width=bar_width,label='ELP-Net', tick_label = tick_label )\n",
    "for a, b in zip(index , ELP_acc_max):\n",
    "    plt.text(a, b + 0.01, '%.3f' % b, ha='center', va='bottom', fontsize=10)\n",
    "\n",
    "\n",
    "plt.legend(loc='upper center') #loc='upper center', bbox_to_anchor=(0.8,0.5),ncol=1,fancybox=True,shadow=False)\n",
    "plt.xlim(-0.5,7);\n",
    "plt.ylim(0,0.7);\n",
    "plt.tick_params(labelsize=7)\n",
    "plt.xlabel(\"pieces\") \n",
    "plt.ylabel(\"accuracy\") \n",
    "plt.show()\n",
    "fig.savefig('acc_piece.png',format='png')\n",
    "fig.savefig('acc_piece.eps',format='eps')\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'plt' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m fig \u001b[38;5;241m=\u001b[39m \u001b[43mplt\u001b[49m\u001b[38;5;241m.\u001b[39mfigure()\n\u001b[0;32m      2\u001b[0m title_label \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTanks_1\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTanks_2\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mArmored vehicles_1\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mArmored vehicles_2\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mSoldiers_1\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mSoldiers_2\u001b[39m\u001b[38;5;124m'\u001b[39m]; \n\u001b[0;32m      5\u001b[0m acc \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mmean(ELP_acc_n_step, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'plt' is not defined"
     ]
    }
   ],
   "source": [
    "\n",
    "fig = plt.figure()\n",
    "title_label = ['Tanks_1','Tanks_2','Armored vehicles_1','Armored vehicles_2','Soldiers_1','Soldiers_2']; \n",
    "\n",
    "\n",
    "acc = np.mean(ELP_acc_n_step, axis=0)\n",
    "\n",
    "   \n",
    "\n",
    "plt.xlabel(\"stages\") \n",
    "plt.ylabel(\"accuracy\") \n",
    "x_index = np.arange(1,11)\n",
    "\n",
    "plt.plot(x_index, acc, '*-', label='ELP-Net') \n",
    "\n",
    "plt.legend()\n",
    "plt.axis([0, 11, 0, 0.8])\n",
    "plt.xticks(x_index)\n",
    "plt.show()\n",
    "fig.savefig('acc_step.png',format='png')\n",
    "fig.savefig('acc_step.eps',format='eps')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_head = '../Baselines/EnemyPositionPrediction/checkpoints'\n",
    "\n",
    "ELP_result = '/ELPNet/full/all/test_pro/test_result'\n",
    "\n",
    "\n",
    "with open(file_head + ELP_result,'rb') as f:\n",
    "    ELP_result_file = pickle.load(f)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_test_pro_result(result, steps=10, offset=0):\n",
    "    action_pres, action_gts = result\n",
    "    # print(action_pres)\n",
    "    # print(action_gts)\n",
    "\n",
    "    ################################## Calc Acc #########################################\n",
    "    \n",
    "    action_pres_np = np.array(action_pres, dtype=object)\n",
    "    \n",
    "    # print(action_pres_np)\n",
    "    action_gts_np = np.array(action_gts, dtype=object)\n",
    "    \n",
    "    # print(action_pres_np.shape, action_gts_np.shape)\n",
    "    acc_type = [1,3,5,10,15,50]\n",
    "    action_acc = []\n",
    "    for piece in range(6):\n",
    "        acc_piece = []\n",
    "        for replay in range(action_pres_np.shape[1]):\n",
    "            acc_replay = []\n",
    "            action_pres = action_pres_np[piece, replay]\n",
    "            action_gts = action_gts_np[piece, replay]\n",
    "            for i, num in enumerate(acc_type):\n",
    "                \n",
    "                acc_replay.append(np.mean([pos in action_pres[j, -num:]\n",
    "                                           for j, pos in enumerate(action_gts[offset:])]))\n",
    "            acc_piece.append(acc_replay)\n",
    "    \n",
    "        action_acc.append(acc_piece)\n",
    "    action_acc = np.array(action_acc)\n",
    "    print(action_acc.shape)\n",
    "    action_acc = action_acc.mean(axis=1)\n",
    "    print('\\tAction Accuracy: {}\\t'.format(action_acc))\n",
    "\n",
    "\n",
    "#     print(Y, Y[:,0].shape, X.shape)\n",
    "    \n",
    "    return action_acc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6, 220, 6)\n",
      "\tAction Accuracy: [[0.29818145 0.42728855 0.50075724 0.61419573 0.69557932 0.86420383]\n",
      " [0.26873069 0.40273118 0.47728406 0.60592602 0.68803942 0.88121118]\n",
      " [0.28821319 0.38587742 0.45958385 0.57804725 0.66334956 0.8603927 ]\n",
      " [0.30266645 0.4316171  0.50411864 0.61897592 0.69699349 0.89640833]\n",
      " [0.2998503  0.39664106 0.4654846  0.58527766 0.67111039 0.86409127]\n",
      " [0.31760039 0.44149625 0.51925503 0.63572589 0.72398123 0.90570924]]\t\n"
     ]
    }
   ],
   "source": [
    "ELP_pre = ELP_result_file['action_pre_per_replay']\n",
    "ELP_gt = ELP_result_file['action_gt_per_replay']\n",
    "ELP_acc = show_test_pro_result((ELP_pre, ELP_gt), offset=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'np' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[6], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m N \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m6\u001b[39m\n\u001b[1;32m----> 2\u001b[0m index \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241m.\u001b[39marange(\u001b[38;5;241m0\u001b[39m, N, \u001b[38;5;241m1\u001b[39m)\n\u001b[0;32m      3\u001b[0m label \u001b[38;5;241m=\u001b[39m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtank_1\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtank_2\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124marmored\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mvehicle_1\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124marmored\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mvehicle_2\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msoldier_1\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msoldier_2\u001b[39m\u001b[38;5;124m'\u001b[39m]\n\u001b[0;32m      6\u001b[0m \u001b[38;5;66;03m# 创建一个3行2列的子图网格\u001b[39;00m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'np' is not defined"
     ]
    }
   ],
   "source": [
    "N = 6\n",
    "index = np.arange(0, N, 1)\n",
    "label = ['tank_1','tank_2','armored\\nvehicle_1','armored\\nvehicle_2','soldier_1','soldier_2']\n",
    "\n",
    "\n",
    "# 创建一个3行2列的子图网格\n",
    "fig, axs = plt.subplots(3, 2, figsize=(10, 7))  # 可以根据需要调整figsize\n",
    "\n",
    "# 绘制每个子图\n",
    "for i in range(6):\n",
    "    row = (i // 2)  # 计算行位置\n",
    "    col = (i % 2)  # 计算列位置\n",
    "    ax = axs[row, col]\n",
    "    \n",
    "    tick_label = ['top 1','top 3','top 5','top 10','top 15','top 50']\n",
    "    bar_width = 0.3\n",
    "    \n",
    "    ax.bar(index, ELP_acc[i], width=bar_width, label='ELP-Net', tick_label=tick_label)\n",
    "    for a, b in zip(index, ELP_acc[i]):\n",
    "        ax.text(a, b + 0.01, '%.3f' % b, ha='center', va='bottom', fontsize=8)\n",
    "    \n",
    "    ax.set_xlim(-0.5, N-0.5)\n",
    "    ax.set_ylim(0, 1)\n",
    "    ax.tick_params(labelsize=9)\n",
    "    ax.set_xlabel(\"test dataset accuracy\")\n",
    "    ax.set_ylabel(label[i], rotation='horizontal', verticalalignment='center', horizontalalignment='right')\n",
    "\n",
    "# 调整子图间的间距\n",
    "plt.tight_layout()\n",
    "\n",
    "# 显示图例\n",
    "# 由于图例可能重叠，我们可以选择只在最后一个子图上显示图例\n",
    "axs[2, 1].legend(bbox_to_anchor=(1.05, 4), loc='upper center', fontsize=10)\n",
    "\n",
    "# 显示图形\n",
    "plt.show()\n",
    "\n",
    "# 保存图形\n",
    "fig.savefig('acc_compare.png', format='png')\n",
    "fig.savefig('acc_compare.eps', format='eps')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'np' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[7], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m ELP_acc_top_means \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241m.\u001b[39mmean(ELP_acc, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m)\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28mprint\u001b[39m(ELP_acc_top_means)\n\u001b[0;32m      4\u001b[0m N \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m6\u001b[39m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'np' is not defined"
     ]
    }
   ],
   "source": [
    "ELP_acc_top_means = np.mean(ELP_acc, axis=0)\n",
    "\n",
    "print(ELP_acc_top_means)\n",
    "N = 6\n",
    "fig = plt.figure()\n",
    "index = np.arange(0, N, 1)\n",
    "\n",
    "tick_label = ['top 1','top 3','top 5','top 10','top 15','top 50']; \n",
    "bar_width = 0.15\n",
    "plt.bar(index , ELP_acc_top_means, width=bar_width,label='ELP-Net', tick_label = tick_label)\n",
    "\n",
    "\n",
    "for a, b in zip(index  , ELP_acc_top_means):\n",
    "    plt.text(a, b + 0.01, '%.3f' % b, ha='center', va='bottom', fontsize=10)\n",
    "\n",
    "\n",
    "plt.legend(loc='upper left') #loc='upper center', bbox_to_anchor=(0.8,0.5),ncol=1,fancybox=True,shadow=False)\n",
    "plt.xlim(-0.5,6)\n",
    "plt.ylim(0,1)\n",
    "plt.tick_params(labelsize=7)\n",
    "plt.xlabel(\"test top mean accuracy\") \n",
    "plt.ylabel(\"accuracy\") \n",
    "plt.show()\n",
    "fig.savefig('ELP_acc_top_means.png',format='png')\n",
    "fig.savefig('ELP_acc_top_means.eps',format='eps')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
